<?php 
class Survey
{
	public $Id;
	public $SiteId;
	public $Title;
	public $Password;
	public $Description;
	public $DescriptionPrint;
	public $Guide;
	public $Active;
	public $Section;
	public $Sections;
	public $Demogs;
	public $DemogValues;
	public $DemogsPrint;
	public $SubLoaded;
	public $Response;
	public $Responses;
	public $ResponsesCount;
	public $ResponsesDemogCount;
	public $DataChanged;
	
	public function getId() { return $this->Id; }
	public function getSiteId() { return $this->SiteId; }
	public function getTitle() { return $this->Title; }
	public function getPassword() { return $this->Password; }
	public function getDescription() { return $this->Description; }
	public function getGuide() { return $this->Guide; }
	public function getActive() { return $this->Active; }
	public function getSection() { return $this->Section; }
	public function getSections() { return $this->Sections; }
	public function getDemogs() { return $this->Demogs; }
	public function getDemogValues() { return $this->DemogValues; }
	public function getSubLoaded() { return $this->SubLoaded; }
	public function getResponse() { return $this->Response; }
	public function getResponses() { return $this->Responses; }
	public function getResponsesCount() 
	{ 
		if(isset($this->ResponsesCount))
		{ return $this->ResponsesCount; }
		else
		{ 
			self::loadResponsesCount(); 
			return $this->ResponsesCount;
		}
	}
	public function getResponsesDemogCount($value) 
	{ 
		if($value > 0)
		{
			if(isset($this->ResponsesDemogCount[$value]))
			{ return $this->ResponsesDemogCount[$value]; }
			else
			{ 
				self::loadResponsesDemogCount($value); 
				return $this->ResponsesDemogCount[$value];
			}
		}
		else
		{ self::getResponsesCount(); }
	}
	public function getDataChanged() { return $this->DataChanged; }
	
	public function setId($val) { $this->Id=$val; }
	public function setSiteId($val) { $this->SiteId=$val; }
	public function setTitle($val) { $this->Title=$val; }
	public function setPassword($val) { $this->Password=$val; }
	public function setDescription($val) { $this->Description=$val; }
	public function setDescriptionPrint($val) { $this->DescriptionPrint=$val; }
	public function setGuide($val) { $this->Guide=$val; }
	public function setActive($val) { $this->Active=$val; }
	public function setSection($val) { $this->Section=$val; }
	public function setSections($val) { $this->Sections=$val; }
	public function setDemogs($val) { $this->Demogs=$val; }
	public function setDemogValues($val) { $this->DemogValues=$val; }
	public function setDemogsPrint($val) { $this->DemogsPrint=$val; }
	public function setSubLoaded($val) { $this->SubLoaded=$val; }
	public function setResponse($val) { $this->Response=$val; }
	public function setResponses($val) { $this->Responses=$val; }
	public function setResponsesCount($val) { $this->ResponsesCount=$val; }
	public function setDataChanged($val) { $this->DataChanged=$val; }
	
	public function Survey() 
	{ 
		self::setDataChanged(false);
		self::setSubLoaded(false); 
	}
	
	public function loadSections()
	{
		if(($this->Id > 0) && ($this->SubLoaded == false))
		{
			unset($this->Sections);
			//Loading only sections that have questions
			$sql  = 'SELECT `id`, `section_type`, `section_order`, `active`, `title`, `guide` FROM '.DB_PERFIX.'survey_sections ';
			$sql .= 'WHERE survey_id='.$this->Id.' AND id IN (SELECT `section_id` FROM '.DB_PERFIX.'survey_question WHERE survey_id='.$this->Id.' AND active=1) AND active=1 ';
			$sql .= 'ORDER BY `section_order`, `id`;';
			$result = mysql_query($sql);
			if($result)
			{
				while($row = mysql_fetch_array($result))
				{
					$this->Sections[$row['id']] = new SurveySection();
					$this->Sections[$row['id']]->setId($row['id']);
					$this->Sections[$row['id']]->setSurveyId($this->Id);
					$this->Sections[$row['id']]->setType($row['section_type']);
					$this->Sections[$row['id']]->setOrder($row['section_order']);
					$this->Sections[$row['id']]->setActive($row['active']);
					$this->Sections[$row['id']]->setTitle($row['title']);
					$this->Sections[$row['id']]->setGuide($row['guide']);
				}
			}
			self::loadQuestions();
		}
		self::setSubLoaded(true);
	}
	
	public function loadQuestions()
	{
		if(($this->Id > 0) && ($this->Sections))
		{
			$sql  = 'SELECT `id`, `survey_id`, `section_id`, `question_type`, `question`, `min_val`, `max_val`, `question_order`, `active` ';
			$sql .= 'FROM '.DB_PERFIX.'survey_question WHERE survey_id='.$this->Id.' AND active=1 ORDER BY `section_id`, `question_order`, `id` ASC;';
			
			$result = mysql_query($sql);
			if($result)
			{
				while($row = mysql_fetch_array($result))
				{
					if($this->Sections[$row['section_id']])
					{
						$this->Sections[$row['section_id']]->Questions[$row['id']] = new SurveyQuestion();
						$this->Sections[$row['section_id']]->Questions[$row['id']]->setId($row['id']);
						$this->Sections[$row['section_id']]->Questions[$row['id']]->setSurveyId($this->Id);
						$this->Sections[$row['section_id']]->Questions[$row['id']]->setSectionId($row['section_id']);
						$this->Sections[$row['section_id']]->Questions[$row['id']]->setType($row['question_type']);
						$this->Sections[$row['section_id']]->Questions[$row['id']]->setOrder($row['question_order']);
						$this->Sections[$row['section_id']]->Questions[$row['id']]->setActive($row['active']);
						$this->Sections[$row['section_id']]->Questions[$row['id']]->setQuestion($row['question']);
						$this->Sections[$row['section_id']]->Questions[$row['id']]->setMinValue($row['min_val']);
						$this->Sections[$row['section_id']]->Questions[$row['id']]->setMaxValue($row['max_val']);
					}
					
				}
			}
			$this->setSubLoaded(true);
		}
	}
	
	public function loadDemogs()
	{
		if($this->Id > 0)
		{
			unset($this->Demogs);
			$sql = 'SELECT `id`, `title`, `active` FROM '.DB_PERFIX.'survey_demography WHERE survey_id='.$this->Id.' ORDER BY `title` ASC;';
			$result = mysql_query($sql);
			if($result)
			{
				while($row = mysql_fetch_array($result))
				{
					$this->Demogs[$row['id']] = new SurveyDemography();
					$this->Demogs[$row['id']]->setId($row['id']);
					$this->Demogs[$row['id']]->setSurveyId($this->Id);
					$this->Demogs[$row['id']]->setActive($row['active']);
					$this->Demogs[$row['id']]->setTitle($row['title']);
				}
			}
			self::loadDemogValues();
		}
	}
	
	public function loadDemogValues()
	{
		if($this->Id > 0)
		{
			if($this->Demogs)
			{
				$sql = 'SELECT `id`, `demography_id`, `value`, `active` FROM '.DB_PERFIX.'survey_demography_values WHERE survey_id='.$this->Id.' AND active=1 ORDER BY `demography_id`, `id` ASC;';
				$result = mysql_query($sql);
				if($result)
				{
					while($row = mysql_fetch_array($result))
					{
						if($this->Demogs[$row['demography_id']])
						{ 
							$this->Demogs[$row['demography_id']]->Values[$row['id']] = new DemographyValue();
							$this->Demogs[$row['demography_id']]->Values[$row['id']]->setId($row['id']);
							$this->Demogs[$row['demography_id']]->Values[$row['id']]->setSurveyId($this->Id);
							$this->Demogs[$row['demography_id']]->Values[$row['id']]->setDemogId($row['demography_id']);
							$this->Demogs[$row['demography_id']]->Values[$row['id']]->setValue($row['value']);
							$this->Demogs[$row['demography_id']]->Values[$row['id']]->setActive($row['active']);
							$this->Demogs[$row['demography_id']]->setSubLoaded(true);
						}
					}
				}
			}
		}
	}
	
	public function getDescriptionPrint() 
	{
		if(isset($this->DescriptionPrint))
		{ return $this->DescriptionPrint; }
		else
		{
			if($this->Description)
			{
				if((substr_count($this->Description,'[dem]') > 0) && ($this->Demogs))
				{ $this->DescriptionPrint = str_ireplace('[dem]',self::getDemogsPrint(),$this->Description); }
				else
				{ $this->DescriptionPrint = $this->Description; }
			}
			return $this->DescriptionPrint; 
		}
	}
	
	public function getDemogsPrint()
	{
		if(isset($this->DemogsPrint))
		{ return $this->DemogsPrint; }
		else
		{
			if($this->Demogs)
			{
				$this->DemogsPrint = '<table cellpadding="0" border="0" cellspacing="0">';
				foreach($this->Demogs as $demog)
				{
					$this->DemogsPrint .= '<tr>';
					$this->DemogsPrint .= 	'<td valign="" align="left">'.$demog->Title.'</td>';
					$this->DemogsPrint .= 	'<td valign="" align="right">&nbsp;&nbsp;<img src="'.SYSICONS_URL.'spacer.gif" width="16" height="16" border="2" /></td>';
					$this->DemogsPrint .= '</tr>';
				}
				$this->DemogsPrint .= '</table>';
			}
			return $this->DemogsPrint;
		}
	}
	
	public function loadResponsesCount()
	{
		$this->ResponsesCount = 0;
		if($this->Id > 0)
		{
			$sql = 'SELECT COUNT(id) AS responses FROM '.DB_PERFIX.'survey_response WHERE survey_id='.$this->Id.';';
			$result = mysql_query($sql);
			if($result)
			{
				while($row = mysql_fetch_array($result))
				{ $this->ResponsesCount = $row['responses']; }
			}
		}
	}
}
?>